package cglgu.trellis.chado.das2.modelimpl;

/*
 * $Id: ChadoSourcesCapability.java 191 2011-03-27 00:38:22Z sundaram.georgetown@gmail.com $
 * $Author: sundaram.georgetown@gmail.com $
 * $Date: 2011-03-27 00:38:22 +0000 (Sun, 27 Mar 2011) $
 * $Rev: 191 $
 * $HeadURL: http://genomancer.googlecode.com/svn/branches/gu-dev-branch/tengcha/src/cglgu/trellis/chado/das2/modelimpl/ChadoSourcesCapability.java $
 */

import genomancer.trellis.das2.model.Das2CoordinatesI;
import genomancer.trellis.das2.model.Das2SourceI;
import genomancer.trellis.das2.model.Das2SourcesCapabilityI;
import genomancer.trellis.das2.model.Das2SourcesResponseI;
import genomancer.trellis.das2.model.Das2VersionI;
import genomancer.util.TimeCheck;
import genomancer.vine.das2.client.modelimpl.Das2Coordinates;
import genomancer.vine.das2.client.modelimpl.Das2GenericCapability;
import genomancer.vine.das2.client.modelimpl.Das2SourcesResponse;
import cglgu.trellis.chado.das2.cglgu.registry.ManagerFactory;
import cglgu.trellis.chado.das2.cglgu.db.DbUtilFactory;
import cglgu.trellis.chado.das2.cglgu.db.CglguDbUtil;
import cglgu.trellis.chado.das2.cglgu.Project;
import java.sql.Statement;
import java.net.URI;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class ChadoSourcesCapability extends Das2GenericCapability 
implements Das2SourcesCapabilityI  {

	static public int max_seqs_allowed = 100;

	Das2SourcesResponseI sources = null;
	Map<URI, Das2SourceI> uri2source = null;
	Map<URI, Das2VersionI> uri2version = null;
	Map<String, ChadoSource> name2source = new HashMap<String, ChadoSource>();
	Map<String, ChadoVersion> name2version = new HashMap<String, ChadoVersion>();

	public ChadoSourcesCapability(URI base_uri, String query_uri, Map<String, String> params)  {
		super(base_uri, query_uri, "sources", null, null);
	}

	public Das2SourcesResponseI getSources() {
		if (sources == null)  { 
			initSources();
		}
		return sources;
	}

	public Das2SourceI getSource(String name)  { 
		if (sources == null)  { initSources(); }
		return (Das2SourceI)name2source.get(name);
	}

	public Das2VersionI getVersion(String name)  { 
		if (sources == null)  { initSources(); }
		return (Das2VersionI)name2version.get(name);
	}

	public Das2SourceI getSource(URI source_uri)  {
		if (sources == null)  { initSources(); }
		return uri2source.get(source_uri);
	}

	public Das2VersionI getVersion (URI version_uri)  {
		if (sources == null)  { initSources(); }
		return uri2version.get(version_uri);
	}

	protected boolean initSources()  {
		System.out.println("called ChadoSourceCapability.initSources()");
		System.out.println("----- CALLING INITSOURCES() ------");
		boolean success = false;
		List<Das2SourceI> slist = new ArrayList<Das2SourceI>();

		TimeCheck tcheck = new TimeCheck("Das2ServerInfo.initialize()");
		tcheck.start();

		/* Get instance of the RegistryManagerFactory 
		 * The projects' details may be stored in any of the following
		 * types of registries:
		 * 1. relational database
		 * 2. tab-delimited file
		 * 3. XML file
		 * 4. hard-coded in the RegistryManager class
		 * 
		 * */
		regManagerFactory = RegistryManagerFactory.getInstance();		

		/* Get instance of the RegistryManager */
		String managerType = null
		RegistryManager regManager = regManagerFactory.create(managerType);

		/* Get the list of all registered projects */
		List<Project> projectList = regManager.getProjectList();

		/* Iterate over each project and build the data structure 
		 * In poka:
		 * 1. the version is derived from the dbDb.name (database name)
		 * 2. the source is derived from the dbDb.genome (genus, species e.g.: A mellifera)
		 * 
		 * */

		for (Project project : projectList){

			String source_name = project.getGenome();
			String version_name = project.getDatabase();
			String description = project.getDescription();
			CglguDbUtil dbutil = project.getDbUtil();

			ChadoSource chadoSource;

			if (name2source.containsKey(source_name)){
				chadoSource = name2source.get(source_name);						
			}
			else {
				chadoSource = new ChadoSource(this.getBaseURI() , source_name, this);
				name2source.put(source_name, chadoSource);
				slist.add(chadoSource);
			}

			ChadoVersion chadoVersion = new ChadoVersion(chadoSource, version_name, source_name, description); 

			name2version.put(chadoVersion.getName(), chadoVersion);

			// add coordinates
			Das2CoordinatesI coords = new Das2Coordinates(this.getBaseURI(),  
					version_name + "/coords", 
					null,  // taxonomy_id
					"chromosome", 
					"unknown", // authority
					"unknown", // build version
					null,      // created
					null);     // test_range

			chadoVersion.addCoordinates(coords);
			// add segments, types, features capabilities
			//  MUST add in this order
			ChadoSegmentsCapability segcap = new ChadoSegmentsCapability(chadoVersion, coords, dbutil);

			chadoVersion.addCapability(segcap);

			ChadoTypesCapability typecap = new ChadoTypesCapability(new_version, coords, dbutil);

			chadoVersion.addCapability(typecap);

			ChadoFeaturesCapability featcap = new ChadoFeaturesCapability(new_version, coords, dbutil);

			chadoVersion.addCapability(featcap);

			source.addVersion(chadoVersion);
		}
	}
	sources = new Das2SourcesResponse(this.getBaseURI(), slist, null, null);
	uri2source = new HashMap<URI, Das2SourceI>();
	uri2version = new HashMap<URI, Das2VersionI>();
	for (Das2SourceI source : sources.getSources()) {
		uri2source.put(source.getAbsoluteURI(), source);
		uri2source.put(source.getLocalURI(), source);
		for (Das2VersionI version : source.getVersions()) {
			uri2version.put(version.getAbsoluteURI(), version);
			uri2version.put(version.getLocalURI(), version);
		}
	}
	tcheck.report();
	return success;
}
